package com.api.syh56.shengyunhui.mapper;

import com.api.syh56.shengyunhui.entity.Address;
import com.api.syh56.shengyunhui.entity.Quote;
import com.api.syh56.shengyunhui.entity.Waybill;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface AddressMapper extends BaseMapper<Address> {

    /**
     * 插入返回主键
     * @param address
     * @return
     */
    @Insert({ "insert into address(contact_name, province, city, district, address, uid, address_location, telephone,creation_time, last_modification_time) values(" +
            "#{contactName}, \n" +
            "    #{province}, \n" +
            "    #{city}, \n" +
            "    #{district}, \n" +
            "    #{address}, \n" +
            "    #{uid}, \n" +
            "    #{addressLocation}, \n" +
            "    #{telephone}, \n" +
            "    #{creationTime}, \n" +
            "    #{lastModificationTime})" })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertWithId(Address address);


    @Select("select q.* from waybill w left outer join quote q on q.waybill_id = w.id where w.id = #{wid}")
    List<Quote> findQuoteByWaybillId(Long wid);

    @Select("select * from address " +
            "where uid = #{uid}  " +
            "AND is_deleted = 0 " +
            "AND CONCAT(province,city,district,address,telephone,contact_name) like concat('%',#{keyword},'%')")
    Page<Address> selectByKeyword(Page<?> page, Long uid, String keyword);

}
